import csv
import string
import time
from smap.driver import SmapDriver
from smap.util import periodicSequentialCall
import datetime, calendar
import smap.core as core
import smap.util as util

class fremontBmsDriver(SmapDriver):
	
	def setup(self, opts):
		self.f = open('/home/dheinzerling/BSG/bsg on 169.229.130.74/!Projects/IEQ/Monitoring/199 Fremont/BMS data/June 30/VAV 6-30/all_data.csv', 'rb')
		self.data = csv.reader(self.f)
		self.headers = next(self.data)
		self.ids = {}
		
		for i in xrange(1,len(self.headers)):
			ts = self.add_timeseries('/%s' % self.headers[i], 'NA', data_type="double")
			self.ids[i] = ts['uuid']
						
		self.period = opts.get("Period", 1000000)

	def start(self):
		periodicSequentialCall(self.read).start(self.period)
	
	def read(self):
		errorCount = 0
		"""In the reactor thread, send the data to sMAP"""
		for row in self.data:
			datetime = row[0]
			print datetime
			smaptime = calendar.timegm(time.strptime(datetime, '%m/%d/%Y %H:%M:%S')) + 3600*7
			for i in xrange(1,len(self.headers)):
				try:
					self.add(self.ids[i], smaptime, float(row[i]))
				except ValueError:
					errorCount += 1

							
		print errorCount
					
		
		self.f.close()